package com.oocl.dataprocess.printer;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

import com.oocl.dataprocess.comparator.BirthComparator;
import com.oocl.dataprocess.comparator.IdComparator;
import com.oocl.dataprocess.pojo.Customer;

public class Printer {
	private static StringBuilder sb;
	public static final String str ="ID"+"\t\t"+"NAME"+"\t\t"+"EMAIL"+"\t\t"+"SEX"+"\t\t"+"BIRTH\r\n";
	public static String print(String flag,ArrayList<Customer> s){
		if("birth".equalsIgnoreCase(flag)){
			Set<Customer> set = new TreeSet<Customer>(new BirthComparator());
			for(Customer customer: s){
				set.add(customer);
			}
			sb = new StringBuilder();
			sb.append(str);
			System.out.println(str);
			for(Customer c:set){
				System.out.println(c.getId()+"\t\t"+c.getName()+"\t\t"+c.getEmial()+"\t\t"+c.getSex()+"\t\t"+c.getBirth());
				sb.append(c.getId()+"\t\t"+c.getName()+"\t\t"+c.getEmial()+"\t\t"+c.getSex()+"\t\t"+c.getBirth()+"\r\n");
			}
			
		}else if("id".equalsIgnoreCase(flag)){
			Set<Customer> set = new TreeSet<Customer>(new IdComparator());
			sb = new StringBuilder();
			for(Customer customer: s){
				set.add(customer);
			}
			System.out.println(str);
			for(Customer c:set){
				System.out.println(c.getId()+"\t\t"+c.getName()+"\t\t"+c.getEmial()+"\t\t"+c.getSex()+"\t\t"+c.getBirth());
				sb.append(c.getId()+"\t\t"+c.getName()+"\t\t"+c.getEmial()+"\t\t"+c.getSex()+"\t\t"+c.getBirth()+"\r\n");
			}
		}
		return sb.toString();
	}
	public static String printXML(ArrayList<Customer> s,Comparator<Customer> comparator){
		Set<Customer> set = new TreeSet<Customer>(comparator);
		for(Customer customer: s){
			set.add(customer);
		}
		sb = new StringBuilder();
		sb.append("<Customers>\r\n");
		for (Customer customer : set) {
		sb.append("\t<Customer id=\"" + customer.getId() + "\">\r\n").append("\t\t<name>" + customer.getName() + "</name>\r\n").append("\t\t<birth>" + customer.getBirth() + "</birth>\r\n")
		.append("\t\t<sex>" + customer.getSex() + "</sex>\r\n").append("\t\t<email>" + customer.getEmial() + "</email>\r\n").append("\t</Customer>\r\n");
		}
		sb.append("</Customers>\r\n");
		System.out.println(sb.toString());
		return sb.toString();
	}
}
